package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Loader;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	
	
	/**
	 * ImageSprite is a class to represent the loading and drawing of a single
	 * image object onto a sprite-based interface, such as Render.
	 * General use is to first call setPosition() to define the image location
	 * and position, and then use load() to attempt to load the image.
	 * It will draw to the screen when loading is complete.
	 */
	public class CarPart extends Sprite
	{
		public var bitmap:BitmapData;
		public var orbitmap:BitmapData;
		private var wid:int;
		public var hgt:int=100;
		private var r:Sprite;
		private var overrideS:Boolean=false;
		protected var filename:String = "";
		private var xFix:int = 0;
		private var yFix:int = 0;
		private var color:Boolean = false;
		private var colour:uint = 0xFFFFFF;
		
		private var partType:String;
		private var id:int=0;
		
		public function CarPart(fl:String){
			cacheAsBitmap = true;
			partType=fl;
		}
		public function setID(id:int){
			if (id>0)
				id%=CONSTANTS.carTypes;
			if (id<0)id+=CONSTANTS.carTypes;
			load(null,"assets/images/carparts/"+partType+"_"+id+".png");
			this.id=id;
		}
		public function givePosFix(x:int,y:int):void{
			xFix=x;
			yFix=y;
		}
		
		/**
		 * Set the position of the sprite relative to the screen.
		 * 
		 * @param x: The x co-ordinate of the leftmost point of the sprite
		 * @param y: The y co-ordinate of the uppermost point of the sprite
		 */
		public function setPosition(x:int, y:int):void{
			this.x=x+xFix;
			this.y=y+yFix;
		}
		/**
		 * Set the width of the shape to be overriden to. Resizes when loaded.
		 * 
		 * @param w: Width
		 * @param h: currently not needed but there for later incase of double resizing needed
		 */
		public function overrideSize(w:int,h:int):void{
			overrideS=true;
			wid=w;
			hgt=h;
		}
		/**
		 * Begin loading of the image, given the specified url.
		 * 
		 * @param render: The Sprite object that the image will display onto.
		 * @param url: The location of the image file, local or otherwise. 
		 */
		public function load(render:Sprite,url:String):void{
			graphics.clear();
			r=render;
			filename=url;
			loadImage(url);
		}
		public function setColor(c:uint):void{
			colour=c;
		}
		public function paint():void{
			this.removeChildren();

			var filter:Sprite;
			var filter2:Sprite;
			this.blendMode = BlendMode.LAYER;
			filter = new Sprite();
			filter.graphics.beginFill(colour);
			filter.graphics.drawRect(0,0,896,716);
			filter.graphics.endFill();
			filter.blendMode = BlendMode.MULTIPLY;
			filter2 = new Sprite();
			filter.addChild(filter2);
			var mx:Matrix = new Matrix(1,0,0,1,0,0);
			filter2.graphics.beginBitmapFill(bitmap,mx);
			filter2.graphics.drawRect(0,0, bitmap.width, bitmap.height);
			filter2.graphics.endFill();	
			filter2.blendMode = BlendMode.ALPHA;
			this.addChild(filter);
			this.graphics.clear();
			this.graphics.beginBitmapFill(orbitmap,mx);
			this.graphics.drawRect(0,0, bitmap.width, bitmap.height);
			this.graphics.endFill();	
		}
		private function loadImage(url:String):void{
			
			var loader : Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,LOLWHATCUNT);
			loader.load(new URLRequest(url));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoadComplete);
		}
		function LOLWHATCUNT(e:Event):void{bitmap=null;orbitmap=null;graphics.clear();this.removeChildren();}
		/**
		 * This is called when the image has finished loading, and will
		 * display the sprite on the screen at the set position.
		 */
		public function imgLoadComplete(e:Event):void{
			bitmap = new Bitmap(e.target.content.bitmapData).bitmapData;
			var mx:Matrix = new Matrix(1,0,0,1,0,0);
			if (overrideS){
				//resizes image if overrideS is true
				var scale:Number = wid/bitmap.width;
				var matrix:Matrix = new Matrix();
				matrix.scale(scale, scale);
				var smallBMD:BitmapData = new BitmapData(bitmap.width * scale, bitmap.height * scale, true, 0x000000);
				smallBMD.draw(bitmap, matrix, null, null, null, true);
				bitmap = new Bitmap(smallBMD, PixelSnapping.NEVER, true).bitmapData;
			}
			orbitmap=(new Bitmap(bitmap)).bitmapData;
			hgt=bitmap.height;
			paint();			
		}
	}
}